//---------------------------------------------------------------------------


#pragma hdrstop

#include "stdafx.h"
#include "ConrtolBoard.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#define ADDINDEX_BRX(A, B) ((A + B) % BOARD_RX_BUFF_SIZE)

unsigned char Board_Receive[BOARD_R_LENGTH] = {0};
unsigned char Board_Send[BOARD_S_LENGTH] = {0};
BoardRXStruct BoardRX = {0};

void Board_Init(void)
{
	Board_Receive_Init();
	Board_Send_Init();
	BoardRX_Init();
}

void Board_Receive_Init(void)
{
	for(int i=0; i < BOARD_R_LENGTH; i++)
	{
		Board_Receive[i] = 0;
	}
}

void Board_Send_Init(void)
{
	for(int i=0; i < BOARD_S_LENGTH; i++)
	{
		Board_Send[i] = 0;
	}
}

void BoardRX_Init(void)
{
	for(int i=0; i < BOARD_RX_BUFF_SIZE; i++)
	{
		BoardRX.Buff[i] = 0;
	}
	BoardRX.EndIndex = 0;
	BoardRX.NowIndex = 0;
}

bool BoardRX2Receive(void)
{
  int num_nondealed_data = 0;
	int num_read_byte = 0;
	unsigned char checksum;
	AnsiString str_show_rx = "BoardRX:";
	AnsiString str_show_receive = "BoardReceive:";
	num_read_byte = BOARD_PORT->Read(CommBuff, COMM_BUFF_SIZE);

	for(int i=0; i < num_read_byte; i++)
	{
		BoardRX.Buff[BoardRX.EndIndex] = CommBuff[i];
		BoardRX.EndIndex = ADDINDEX_BRX(BoardRX.EndIndex,1);
		#ifdef bShow_BoardRX
			if(bShow_BoardRX)
			{
				str_show_rx = str_show_rx + " " + IntToStr(CommBuff[i]);
			}
		#endif
	}
	#ifdef bShow_BoardRX
		if(bShow_BoardRX)
		{
			Memo_ShowInfo->Lines->Add(str_show_rx);
		}
	#endif

	if (BoardRX.NowIndex <= BoardRX.EndIndex)
	{
		num_nondealed_data = BoardRX.EndIndex - BoardRX.NowIndex;
	}
	else
	{
		num_nondealed_data = BOARD_RX_BUFF_SIZE - (BoardRX.NowIndex - BoardRX.EndIndex);
	}

	while(num_nondealed_data >= BOARD_R_LENGTH)
	{
		if((BoardRX.Buff[ADDINDEX_BRX(BoardRX.NowIndex,BOARD_R_HEAD1)] == 255) &&
			 (BoardRX.Buff[ADDINDEX_BRX(BoardRX.NowIndex,BOARD_R_HEAD2)] == 255))
		{
			checksum = 0;
			for(int i=BOARD_R_DIP; i<BOARD_R_CHECKSUM; i++)
			{
				checksum += BoardRX.Buff[ADDINDEX_BRX(BoardRX.NowIndex,i)];
			}
			checksum = ~checksum;
			if(checksum == BoardRX.Buff[ADDINDEX_BRX(BoardRX.NowIndex,BOARD_R_CHECKSUM)])
			{
				for(int i=BOARD_R_HEAD1; i<BOARD_R_LENGTH; i++)
				{
					Board_Receive[i] = BoardRX.Buff[ADDINDEX_BRX(BoardRX.NowIndex,i)];
					#ifdef bShow_BoardReceive
						if(bShow_BoardReceive)
						{
							str_show_receive = str_show_receive + " " + IntToStr(Board_Receive[i]);
						}
					#endif
				}
				#ifdef bShow_BoardReceive
					if(bShow_BoardReceive)
					{
						Memo_ShowInfo->Lines->Add(str_show_receive);
					}
				#endif

				if(bExecuteInitial == false)
				{
					bExecuteInitial = Board_Receive[BOARD_R_INIT];
				}
				BoardRX.NowIndex = ADDINDEX_BRX(BoardRX.NowIndex,BOARD_R_LENGTH);
//				BoardRX.NowIndex = 0;
//				BoardRX.EndIndex = 0;
				return true;
			}
			else
			{
				BoardRX.NowIndex = ADDINDEX_BRX(BoardRX.NowIndex,1);
			}
		}
		else
		{
			BoardRX.NowIndex = ADDINDEX_BRX(BoardRX.NowIndex,1);
		}

		if (BoardRX.NowIndex <= BoardRX.EndIndex)
		{
			num_nondealed_data = BoardRX.EndIndex - BoardRX.NowIndex;
		}
		else
		{
			num_nondealed_data = BOARD_RX_BUFF_SIZE - (BoardRX.NowIndex - BoardRX.EndIndex);
		}
	}
	return false;
}

void BoardSend(void)
{
	Board_Send[BOARD_S_HEAD1] = 0xFF;
	Board_Send[BOARD_S_HEAD2] = 0xFF;
	Board_Send[BOARD_S_LED1] = 0;
	Board_Send[BOARD_S_LED2] = 0;
	Board_Send[BOARD_S_LED3] = 0;
	Board_Send[BOARD_S_LED4] = 0;
	Board_Send[BOARD_S_LED5] = 0;
	Board_Send[BOARD_S_LED6] = 0;

	Board_Send[BOARD_S_CHECKSUM] = 0;
	for(int i=BOARD_S_HEAD2+1; i < BOARD_S_CHECKSUM; i++)
	{
		Board_Send[BOARD_S_CHECKSUM] += Board_Send[i];
	}
	Board_Send[BOARD_S_CHECKSUM] = ~Board_Send[BOARD_S_CHECKSUM];

	Comm_Write(BOARD_PORT, Board_Send, BOARD_S_LENGTH);
}
